package com.multiplayer.listener;

import javax.servlet.ServletContext;
import javax.servlet.annotation.WebListener;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

/**
 * Application Lifecycle Listener implementation class TestSessionListener
 *
 */
@WebListener
public class TestSessionListener implements HttpSessionListener {

    /**
     * Default constructor. 
     */
    public TestSessionListener() {
        // TODO Auto-generated constructor stub
    }

	/**
     * @see HttpSessionListener#sessionCreated(HttpSessionEvent)
     */
    public void sessionCreated(HttpSessionEvent se)  { 
         // TODO Auto-generated method stub
    	System.out.println("执行sessionCreated");
    	ServletContext application=se.getSession().getServletContext();
    	if(application.getAttribute("count")==null){
    		application.setAttribute("count", 1);
    	}
    	else{
    		Integer count=(Integer) application.getAttribute("count");
    		application.setAttribute("count", count+1);
    	}
    }

	/**
     * @see HttpSessionListener#sessionDestroyed(HttpSessionEvent)
     */
    public void sessionDestroyed(HttpSessionEvent se)  { 
         // TODO Auto-generated method stub
    	System.out.println("执行sessionDestroyed");
    	ServletContext application=se.getSession().getServletContext();
     	if(application.getAttribute("count")!=null){
     		Integer count=(Integer) application.getAttribute("count");
    		application.setAttribute("count", count-1);
    	}
    }
	
}
